package close.mazegame.input;

import java.util.HashMap;

public abstract class BasicInput<T extends Enum<T>> implements Input<T> {
	
	protected HashMap<Integer, T> controlScheme = new HashMap<Integer, T>();
	private HashMap<T, Boolean> pressedKeys = new HashMap<T, Boolean>();
	private HashMap<T, Boolean> typedKeys = new HashMap<T, Boolean>();
	
	protected boolean pressKey(int keycode) {
		if (controlScheme.containsKey(keycode)) {
			setPressed(controlScheme.get(keycode), true);
			setTyped(controlScheme.get(keycode), true);
			return true;
		} else {
			return false;
		}
	}
	
	protected boolean releaseKey(int keycode) {
		if (controlScheme.containsKey(keycode)) {
			setPressed(controlScheme.get(keycode), false);
			return true;
		} else {
			return false;
		}
	}
	
	private void setPressed(T key, boolean state) {
		pressedKeys.put(key, state);
	}
	
	private void setTyped(T key, boolean state) {
		typedKeys.put(key, state);
	}

	@Override
	public boolean isPressed(T key) {
		return pressedKeys.get(key);
	}

	@Override
	public boolean isTyped(T key) {
		return typedKeys.get(key);
	}

	@Override
	public void resetTypedKeys() {
		for (T k : typedKeys.keySet()) {
			typedKeys.put(k, false);
		}
	}

}
